package com.shrct.businessprocess.purchase.impl;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.shrct.businessprocess.budget.RCBudget;
import com.shrct.businessprocess.budget.RCCostBreakDown;
import com.shrct.businessprocess.diagnose.DiagnoseItem;
import com.shrct.businessprocess.diagnose.IDiagnoseAcceptor;
import com.shrct.businessprocess.money.Moneys;
import com.shrct.businessprocess.organization.RCSupplier;
import com.shrct.businessprocess.product.RCIdentifiablePhysicalProductEntry;
import com.shrct.businessprocess.product.RCProductEntry;
import com.shrct.businessprocess.product.RCProductServiceTracerDAO;
import com.shrct.businessprocess.product.RCProductSpecification;
import com.shrct.businessprocess.product.RCServiceLevel;
import com.shrct.businessprocess.product.RCServiceResponseTime;
import com.shrct.businessprocess.project.RCExchangeRate;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.project.RCProjectStat;
import com.shrct.businessprocess.purchase.RCBaseProductPurchaseBudget;
import com.shrct.businessprocess.purchase.RCBasePurchasePayRequest;
import com.shrct.businessprocess.purchase.RCProductPurchaseBudget;
import com.shrct.businessprocess.purchase.RCProductPurchaseRequest;
import com.shrct.businessprocess.purchase.RCPurchaseOrder;
import com.shrct.businessprocess.purchase.RCPurchaseOrderAccept;
import com.shrct.businessprocess.purchase.RCPurchaseOrderAcceptItem;
import com.shrct.businessprocess.purchase.RCPurchaseOrderInvoice;
import com.shrct.businessprocess.purchase.RCPurchaseOrderItem;
import com.shrct.businessprocess.purchase.RCPurchaseOrderStatus;
import com.shrct.businessprocess.purchase.impl.RCBasePurchaseOrderImpl;
import com.shrct.businessprocess.purchase.impl.RCProductPurchaseRequestImpl;
import com.shrct.businessprocess.purchase.impl.RCPurchaseOrderAcceptImpl;
import com.shrct.businessprocess.purchase.impl.RCPurchaseOrderItemImpl;
import com.shrct.businessprocess.purchase.proxy.RCPurchaseOrderProxy;
import edu.fudan.langlab.domain.statemachine.ModelObjectLogService;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.IObjectFactory;
import edu.fudan.mylang.utils.DateUtil;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.BeanFactory;

@Entity
@DiscriminatorValue("RCPURCHASEORDER_TYPE")
public class RCPurchaseOrderImpl extends RCBasePurchaseOrderImpl implements RCPurchaseOrder {
  public RCPurchaseOrderImpl() {
    super();
  }
  
  public RCPurchaseOrderImpl(final RCProductPurchaseRequest purchaseRequest) {
    super();
    this.purchaseRequest = purchaseRequest;
  }
  
  private RCPurchaseOrderStatus Status;
  
  public RCPurchaseOrderStatus getStatus() {
    return this.Status;
  }
  
  private ModelObjectLogService getModelObjectLogService() {
    return getBeanFactory().getBean(ModelObjectLogService.class);
  }
  
  private void _start() {
    
    Status=RCPurchaseOrderStatus.craeted;
  }
  
  public void start() {
    if(Status==null){
    	_start();
    	getModelObjectLogService().createLog(this,"Status","Start");
    	getBeanFactory().getBean(com.shrct.businessprocess.purchase.events.RCPurchaseOrderEventsManager.class).fireStarted(this);					
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException("allready started");
    }
  }
  
  private void _startAudit() {
    
    Status=RCPurchaseOrderStatus.auditing;
  }
  
  public void startAudit() {
    if(Status==RCPurchaseOrderStatus.craeted){
    	_startAudit();
    	getModelObjectLogService().createLog(this,"Status","startAudit");
    	getBeanFactory().getBean(com.shrct.businessprocess.purchase.events.RCPurchaseOrderEventsManager.class).fireStartAudit(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  private void _auditPassed() {
    
    this.setIsInitialized(Boolean.valueOf(true));
    Date _day = DateUtil.today();
    this.setInitializeDate(_day);
    this.purchaseRequest.calcIsOrderFinished();
    Status=RCPurchaseOrderStatus.executing;
  }
  
  public void auditPassed() {
    if(Status==RCPurchaseOrderStatus.auditing){
    	_auditPassed();
    	getModelObjectLogService().createLog(this,"Status","auditPassed");
    	getBeanFactory().getBean(com.shrct.businessprocess.purchase.events.RCPurchaseOrderEventsManager.class).fireAuditPassed(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  private void _auditCanceled() {
    
    Status=RCPurchaseOrderStatus.canceled;
  }
  
  public void auditCanceled() {
    if(Status==RCPurchaseOrderStatus.auditing){
    	_auditCanceled();
    	getModelObjectLogService().createLog(this,"Status","auditCanceled");
    	getBeanFactory().getBean(com.shrct.businessprocess.purchase.events.RCPurchaseOrderEventsManager.class).fireAuditCanceled(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  private void _acceptItem() {
    
    Status=RCPurchaseOrderStatus.executing;
  }
  
  public void acceptItem() {
    if(Status==RCPurchaseOrderStatus.executing){
    	_acceptItem();
    	getModelObjectLogService().createLog(this,"Status","acceptItem");
    	getBeanFactory().getBean(com.shrct.businessprocess.purchase.events.RCPurchaseOrderEventsManager.class).fireAcceptItem(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  private void _invoiceRegister() {
    
    Status=RCPurchaseOrderStatus.executing;
  }
  
  public void invoiceRegister() {
    if(Status==RCPurchaseOrderStatus.executing){
    	_invoiceRegister();
    	getModelObjectLogService().createLog(this,"Status","invoiceRegister");
    	getBeanFactory().getBean(com.shrct.businessprocess.purchase.events.RCPurchaseOrderEventsManager.class).fireInvoiceRegister(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  private void _payRequest() {
    
    Status=RCPurchaseOrderStatus.executing;
  }
  
  public void payRequest() {
    if(Status==RCPurchaseOrderStatus.executing){
    	_payRequest();
    	getModelObjectLogService().createLog(this,"Status","payRequest");
    	getBeanFactory().getBean(com.shrct.businessprocess.purchase.events.RCPurchaseOrderEventsManager.class).firePayRequest(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  private void _close() {
    
    this.setIsClosed(Boolean.valueOf(true));
    Status=RCPurchaseOrderStatus.closed;
  }
  
  public void close() {
    if(Status==RCPurchaseOrderStatus.executing){
    	_close();
    	getModelObjectLogService().createLog(this,"Status","close");
    	getBeanFactory().getBean(com.shrct.businessprocess.purchase.events.RCPurchaseOrderEventsManager.class).fireClose(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  private void _postCancel() {
    
    boolean _or = false;
    boolean _or_1 = false;
    int _size = this.acceptItems.size();
    boolean _notEquals = (_size != 0);
    if (_notEquals) {
      _or_1 = true;
    } else {
      Collection<RCBasePurchasePayRequest> _payRequests = this.getPayRequests();
      int _size_1 = _payRequests.size();
      boolean _notEquals_1 = (_size_1 != 0);
      _or_1 = (_notEquals || _notEquals_1);
    }
    if (_or_1) {
      _or = true;
    } else {
      Collection<RCPurchaseOrderInvoice> _invoices = this.getInvoices();
      int _size_2 = _invoices.size();
      boolean _notEquals_2 = (_size_2 != 0);
      _or = (_or_1 || _notEquals_2);
    }
    if (_or) {
      RuntimeException _runtimeException = new RuntimeException("Invalid state");
      throw _runtimeException;
    }
    this.setIsInitialized(Boolean.valueOf(false));
    this.setInitializeDate(null);
    Status=RCPurchaseOrderStatus.canceled;
  }
  
  public void postCancel() {
    if(Status==RCPurchaseOrderStatus.executing){
    	_postCancel();
    	getModelObjectLogService().createLog(this,"Status","postCancel");
    	getBeanFactory().getBean(com.shrct.businessprocess.purchase.events.RCPurchaseOrderEventsManager.class).firePostCancel(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  private void _cancelClose() {
    
    this.setIsClosed(Boolean.valueOf(false));
    RCBaseProductPurchaseBudget _budget = this.purchaseRequest.getBudget();
    RCCostBreakDown _cbd = _budget.getCbd();
    RCProject _project = _cbd.getProject();
    RCProjectStat _stat = _project.getStat();
    _stat.updateInfo();
    Status=RCPurchaseOrderStatus.executing;
  }
  
  public void cancelClose() {
    if(Status==RCPurchaseOrderStatus.closed){
    	_cancelClose();
    	getModelObjectLogService().createLog(this,"Status","cancelClose");
    	getBeanFactory().getBean(com.shrct.businessprocess.purchase.events.RCPurchaseOrderEventsManager.class).fireCancelClose(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  public Boolean isCraeted() {
    return com.shrct.businessprocess.purchase.RCPurchaseOrderStatus.craeted==Status;
  }
  
  public Boolean isAuditing() {
    return com.shrct.businessprocess.purchase.RCPurchaseOrderStatus.auditing==Status;
  }
  
  public Boolean isExecuting() {
    return com.shrct.businessprocess.purchase.RCPurchaseOrderStatus.executing==Status;
  }
  
  public Boolean isClosed() {
    return com.shrct.businessprocess.purchase.RCPurchaseOrderStatus.closed==Status;
  }
  
  public Boolean isCanceled() {
    return com.shrct.businessprocess.purchase.RCPurchaseOrderStatus.canceled==Status;
  }
  
  @JoinColumn(name = "purchaseRequest_id")
  @ManyToOne(targetEntity = RCProductPurchaseRequestImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCProductPurchaseRequest purchaseRequest;
  
  public RCProductPurchaseRequest getPurchaseRequest() {
    return this.purchaseRequest;
  }
  
  public RCPurchaseOrder setPurchaseRequest(final RCProductPurchaseRequest purchaseRequest) {
    this.purchaseRequest = purchaseRequest;
    return this;			
    
  }
  
  @OneToMany(targetEntity = RCPurchaseOrderItemImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "purchaseOrder")
  private Collection<RCPurchaseOrderItem> orderItems = new java.util.ArrayList<com.shrct.businessprocess.purchase.RCPurchaseOrderItem>();;
  
  public Collection<RCPurchaseOrderItem> getOrderItems() {
    return this.orderItems;
  }
  
  public RCPurchaseOrderItem createAndAddtoOrderItems() {
    getOrderItems().size();
    com.shrct.businessprocess.purchase.RCPurchaseOrderItem rCPurchaseOrderItem = new com.shrct.businessprocess.purchase.impl.RCPurchaseOrderItemImpl(this);
    getObjectFactory().create(rCPurchaseOrderItem);
    getOrderItems().add(rCPurchaseOrderItem);
    return rCPurchaseOrderItem;
    
  }
  
  public RCPurchaseOrderItem createAndAddtoOrderItems(final Procedure1<RCPurchaseOrderItem> updater) {
    getOrderItems().size();
    com.shrct.businessprocess.purchase.RCPurchaseOrderItem rCPurchaseOrderItem = new com.shrct.businessprocess.purchase.impl.RCPurchaseOrderItemImpl(this);
    getObjectFactory().create(rCPurchaseOrderItem);
    getOrderItems().add(rCPurchaseOrderItem);
    return rCPurchaseOrderItem;
    
  }
  
  public RCPurchaseOrder removeAllOrderItems() {
    for(RCPurchaseOrderItem obj : this.orderItems){
    	getObjectFactory().delete(obj);
    }
    this.orderItems.clear();
    return this;	
    
  }
  
  public RCPurchaseOrder removeFromOrderItems(final RCPurchaseOrderItem rCPurchaseOrderItem) {
    this.orderItems.remove(rCPurchaseOrderItem);
    getObjectFactory().delete(rCPurchaseOrderItem);
    return this;
    
  }
  
  @OneToMany(targetEntity = RCPurchaseOrderAcceptImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "purchaseOrder")
  private Collection<RCPurchaseOrderAccept> acceptItems = new java.util.ArrayList<com.shrct.businessprocess.purchase.RCPurchaseOrderAccept>();;
  
  public Collection<RCPurchaseOrderAccept> getAcceptItems() {
    return this.acceptItems;
  }
  
  public RCPurchaseOrderAccept createAndAddtoAcceptItems() {
    getAcceptItems().size();
    com.shrct.businessprocess.purchase.RCPurchaseOrderAccept rCPurchaseOrderAccept = new com.shrct.businessprocess.purchase.impl.RCPurchaseOrderAcceptImpl(this);
    getObjectFactory().create(rCPurchaseOrderAccept);
    getAcceptItems().add(rCPurchaseOrderAccept);
    return rCPurchaseOrderAccept;
    
  }
  
  public RCPurchaseOrderAccept createAndAddtoAcceptItems(final Procedure1<RCPurchaseOrderAccept> updater) {
    getAcceptItems().size();
    com.shrct.businessprocess.purchase.RCPurchaseOrderAccept rCPurchaseOrderAccept = new com.shrct.businessprocess.purchase.impl.RCPurchaseOrderAcceptImpl(this);
    getObjectFactory().create(rCPurchaseOrderAccept);
    getAcceptItems().add(rCPurchaseOrderAccept);
    return rCPurchaseOrderAccept;
    
  }
  
  public RCPurchaseOrder removeAllAcceptItems() {
    for(RCPurchaseOrderAccept obj : this.acceptItems){
    	getObjectFactory().delete(obj);
    }
    this.acceptItems.clear();
    return this;	
    
  }
  
  public RCPurchaseOrder removeFromAcceptItems(final RCPurchaseOrderAccept rCPurchaseOrderAccept) {
    this.acceptItems.remove(rCPurchaseOrderAccept);
    getObjectFactory().delete(rCPurchaseOrderAccept);
    return this;
    
  }
  
  public RCBudget getBudget() {
    RCBaseProductPurchaseBudget _budget = this.purchaseRequest.getBudget();
    return _budget;
  }
  
  public RCPurchaseOrderAccept createAcceptItem() {
    RCPurchaseOrderAccept _createAndAddtoAcceptItems = this.createAndAddtoAcceptItems();
    final Procedure1<RCPurchaseOrderAccept> _function = new Procedure1<RCPurchaseOrderAccept>() {
        public void apply(final RCPurchaseOrderAccept it) {
          RCPurchaseOrderImpl.this.acceptItem();
        }
      };
    RCPurchaseOrderAccept _doubleArrow = ObjectExtensions.<RCPurchaseOrderAccept>operator_doubleArrow(_createAndAddtoAcceptItems, _function);
    return _doubleArrow;
  }
  
  public Integer getTotalOrderItemsQuantity() {
    final Function1<RCPurchaseOrderItem,Integer> _function = new Function1<RCPurchaseOrderItem,Integer>() {
        public Integer apply(final RCPurchaseOrderItem it) {
          Integer _quantity = it.getQuantity();
          return _quantity;
        }
      };
    Iterable<Integer> _map = IterableExtensions.<RCPurchaseOrderItem, Integer>map(this.orderItems, _function);
    final Function2<Integer,Integer,Integer> _function_1 = new Function2<Integer,Integer,Integer>() {
        public Integer apply(final Integer i1, final Integer i2) {
          int _plus = ((i1).intValue() + (i2).intValue());
          return Integer.valueOf(_plus);
        }
      };
    Integer _reduce = IterableExtensions.<Integer>reduce(_map, _function_1);
    Integer _nullsafe = NumberExtensions.nullsafe(_reduce);
    return _nullsafe;
  }
  
  public Integer getTotalAcceptItemsQuantity() {
    final Function1<RCPurchaseOrderAccept,Collection<RCPurchaseOrderAcceptItem>> _function = new Function1<RCPurchaseOrderAccept,Collection<RCPurchaseOrderAcceptItem>>() {
        public Collection<RCPurchaseOrderAcceptItem> apply(final RCPurchaseOrderAccept it) {
          Collection<RCPurchaseOrderAcceptItem> _acceptItems = it.getAcceptItems();
          return _acceptItems;
        }
      };
    Iterable<Collection<RCPurchaseOrderAcceptItem>> _map = IterableExtensions.<RCPurchaseOrderAccept, Collection<RCPurchaseOrderAcceptItem>>map(this.acceptItems, _function);
    Iterable<RCPurchaseOrderAcceptItem> _flatten = Iterables.<RCPurchaseOrderAcceptItem>concat(_map);
    final Function1<RCPurchaseOrderAcceptItem,Integer> _function_1 = new Function1<RCPurchaseOrderAcceptItem,Integer>() {
        public Integer apply(final RCPurchaseOrderAcceptItem it) {
          Integer _quantity = it.getQuantity();
          return _quantity;
        }
      };
    Iterable<Integer> _map_1 = IterableExtensions.<RCPurchaseOrderAcceptItem, Integer>map(_flatten, _function_1);
    final Function2<Integer,Integer,Integer> _function_2 = new Function2<Integer,Integer,Integer>() {
        public Integer apply(final Integer i1, final Integer i2) {
          int _plus = ((i1).intValue() + (i2).intValue());
          return Integer.valueOf(_plus);
        }
      };
    Integer _reduce = IterableExtensions.<Integer>reduce(_map_1, _function_2);
    Integer _nullsafe = NumberExtensions.nullsafe(_reduce);
    return _nullsafe;
  }
  
  public Integer getTotalAcceptItemsQuantity(final RCProductSpecification productSpec) {
    final Function1<RCPurchaseOrderAccept,Collection<RCPurchaseOrderAcceptItem>> _function = new Function1<RCPurchaseOrderAccept,Collection<RCPurchaseOrderAcceptItem>>() {
        public Collection<RCPurchaseOrderAcceptItem> apply(final RCPurchaseOrderAccept it) {
          Collection<RCPurchaseOrderAcceptItem> _acceptItems = it.getAcceptItems();
          return _acceptItems;
        }
      };
    Iterable<Collection<RCPurchaseOrderAcceptItem>> _map = IterableExtensions.<RCPurchaseOrderAccept, Collection<RCPurchaseOrderAcceptItem>>map(this.acceptItems, _function);
    Iterable<RCPurchaseOrderAcceptItem> _flatten = Iterables.<RCPurchaseOrderAcceptItem>concat(_map);
    final Function1<RCPurchaseOrderAcceptItem,Boolean> _function_1 = new Function1<RCPurchaseOrderAcceptItem,Boolean>() {
        public Boolean apply(final RCPurchaseOrderAcceptItem it) {
          RCPurchaseOrderItem _orderItem = it.getOrderItem();
          RCProductSpecification _productSpec = _orderItem.getProductSpec();
          boolean _equals = Objects.equal(_productSpec, productSpec);
          return Boolean.valueOf(_equals);
        }
      };
    Iterable<RCPurchaseOrderAcceptItem> _filter = IterableExtensions.<RCPurchaseOrderAcceptItem>filter(_flatten, _function_1);
    final Function1<RCPurchaseOrderAcceptItem,Integer> _function_2 = new Function1<RCPurchaseOrderAcceptItem,Integer>() {
        public Integer apply(final RCPurchaseOrderAcceptItem it) {
          Integer _quantity = it.getQuantity();
          return _quantity;
        }
      };
    Iterable<Integer> _map_1 = IterableExtensions.<RCPurchaseOrderAcceptItem, Integer>map(_filter, _function_2);
    final Function2<Integer,Integer,Integer> _function_3 = new Function2<Integer,Integer,Integer>() {
        public Integer apply(final Integer i1, final Integer i2) {
          int _plus = ((i1).intValue() + (i2).intValue());
          return Integer.valueOf(_plus);
        }
      };
    Integer _reduce = IterableExtensions.<Integer>reduce(_map_1, _function_3);
    Integer _nullsafe = NumberExtensions.nullsafe(_reduce);
    return _nullsafe;
  }
  
  /**
   * 收货总金额
   */
  public RCProjectMoney getTotalAcceptItemsAmount() {
    final Function1<RCPurchaseOrderAccept,RCProjectMoney> _function = new Function1<RCPurchaseOrderAccept,RCProjectMoney>() {
        public RCProjectMoney apply(final RCPurchaseOrderAccept it) {
          RCProjectMoney _totalAmount = it.getTotalAmount();
          return _totalAmount;
        }
      };
    Iterable<RCProjectMoney> _map = IterableExtensions.<RCPurchaseOrderAccept, RCProjectMoney>map(this.acceptItems, _function);
    RCExchangeRate _defaultExchangeRate = this.getDefaultExchangeRate();
    RCProjectMoney _sum = Moneys.sum(_map, _defaultExchangeRate);
    return _sum;
  }
  
  /**
   * 订单总金额
   */
  public RCProjectMoney getTotalCost() {
    final Function1<RCPurchaseOrderItem,RCProjectMoney> _function = new Function1<RCPurchaseOrderItem,RCProjectMoney>() {
        public RCProjectMoney apply(final RCPurchaseOrderItem it) {
          RCProjectMoney _price = it.getPrice();
          return _price;
        }
      };
    Iterable<RCProjectMoney> _map = IterableExtensions.<RCPurchaseOrderItem, RCProjectMoney>map(this.orderItems, _function);
    RCExchangeRate _defaultExchangeRate = this.getDefaultExchangeRate();
    RCProjectMoney _sum = Moneys.sum(_map, _defaultExchangeRate);
    return _sum;
  }
  
  /**
   * 所有订单项目都已经到货，所有的钱都已经支付
   */
  public Boolean getIsFinished() {
    boolean _and = false;
    RCProjectMoney _totalCost = this.getTotalCost();
    RCProjectMoney _totalAcceptItemsAmount = this.getTotalAcceptItemsAmount();
    boolean _equals = Objects.equal(_totalCost, _totalAcceptItemsAmount);
    if (!_equals) {
      _and = false;
    } else {
      RCProjectMoney _totalCost_1 = this.getTotalCost();
      RCProjectMoney _totalPayedProjectAmount = this.getTotalPayedProjectAmount();
      boolean _equals_1 = Objects.equal(_totalCost_1, _totalPayedProjectAmount);
      _and = (_equals && _equals_1);
    }
    return Boolean.valueOf(_and);
  }
  
  public Boolean getIsAllItemsAccepted() {
    RCProjectMoney _totalCost = this.getTotalCost();
    RCProjectMoney _totalAcceptItemsAmount = this.getTotalAcceptItemsAmount();
    boolean _equals = Objects.equal(_totalCost, _totalAcceptItemsAmount);
    return Boolean.valueOf(_equals);
  }
  
  public Iterable<RCProductEntry> getAllAcceptedProducts() {
    final Function1<RCPurchaseOrderItem,Collection<RCProductEntry>> _function = new Function1<RCPurchaseOrderItem,Collection<RCProductEntry>>() {
        public Collection<RCProductEntry> apply(final RCPurchaseOrderItem it) {
          Collection<RCProductEntry> _allAcceptedProducts = it.getAllAcceptedProducts();
          return _allAcceptedProducts;
        }
      };
    Iterable<Collection<RCProductEntry>> _map = IterableExtensions.<RCPurchaseOrderItem, Collection<RCProductEntry>>map(this.orderItems, _function);
    Iterable<RCProductEntry> _flatten = Iterables.<RCProductEntry>concat(_map);
    return _flatten;
  }
  
  public void acceptProductService(final String name, final String serviceSpec, final String serviceContractCode, final RCSupplier productSupplier, final RCIdentifiablePhysicalProductEntry product, final RCServiceLevel serviceLevel, final RCServiceResponseTime serviceResponseTime, final Date startDate, final Date dueDate, final RCProjectMoney price, final Boolean isPartDevice) {
    BeanFactory _beanFactory = this.getBeanFactory();
    RCProductServiceTracerDAO _bean = _beanFactory.<RCProductServiceTracerDAO>getBean(RCProductServiceTracerDAO.class);
    RCBaseProductPurchaseBudget _budget = this.purchaseRequest.getBudget();
    _bean.acceptProductService(((RCProductPurchaseBudget) _budget), name, serviceSpec, serviceContractCode, productSupplier, product, serviceLevel, serviceResponseTime, startDate, dueDate, price, isPartDevice);
  }
  
  public void diagnose(final IDiagnoseAcceptor acceptor) {
    final Procedure1<RCPurchaseOrderAccept> _function = new Procedure1<RCPurchaseOrderAccept>() {
        public void apply(final RCPurchaseOrderAccept accept) {
          Boolean _isTermOverDue = accept.isTermOverDue();
          if ((_isTermOverDue).booleanValue()) {
            DiagnoseItem _diagnoseItem = new DiagnoseItem();
            final Procedure1<DiagnoseItem> _function = new Procedure1<DiagnoseItem>() {
                public void apply(final DiagnoseItem it) {
                  String _orderNumber = RCPurchaseOrderImpl.this.getOrderNumber();
                  String _plus = ("\u8BA2\u5355[" + _orderNumber);
                  String _plus_1 = (_plus + "]\u6536\u8D27\u7684\u5F85\u4ED8\u6B3E\u8D85\u51FA\u8D26\u671F");
                  it.setMessage(_plus_1);
                }
              };
            DiagnoseItem _doubleArrow = ObjectExtensions.<DiagnoseItem>operator_doubleArrow(_diagnoseItem, _function);
            acceptor.accept(_doubleArrow);
          }
        }
      };
    IterableExtensions.<RCPurchaseOrderAccept>forEach(this.acceptItems, _function);
    final Procedure1<RCPurchaseOrderItem> _function_1 = new Procedure1<RCPurchaseOrderItem>() {
        public void apply(final RCPurchaseOrderItem orderItem) {
          Boolean _isExpectDateOverDue = orderItem.getIsExpectDateOverDue();
          if ((_isExpectDateOverDue).booleanValue()) {
            DiagnoseItem _diagnoseItem = new DiagnoseItem();
            final Procedure1<DiagnoseItem> _function = new Procedure1<DiagnoseItem>() {
                public void apply(final DiagnoseItem it) {
                  String _orderNumber = RCPurchaseOrderImpl.this.getOrderNumber();
                  String _plus = ("\u8BA2\u5355[" + _orderNumber);
                  String _plus_1 = (_plus + "]\u4E2D\u4EA7\u54C1[");
                  RCProductSpecification _productSpec = orderItem.getProductSpec();
                  String _entityName = _productSpec.getEntityName();
                  String _plus_2 = (_plus_1 + _entityName);
                  String _plus_3 = (_plus_2 + "]\u6536\u8D27\u5EF6\u671F");
                  it.setMessage(_plus_3);
                }
              };
            DiagnoseItem _doubleArrow = ObjectExtensions.<DiagnoseItem>operator_doubleArrow(_diagnoseItem, _function);
            acceptor.accept(_doubleArrow);
          }
        }
      };
    IterableExtensions.<RCPurchaseOrderItem>forEach(this.orderItems, _function_1);
  }
  
  public static RCPurchaseOrder create(final RCProductPurchaseRequest purchaseRequest, final IObjectFactory objectFactory) {
    com.shrct.businessprocess.purchase.RCPurchaseOrder rCPurchaseOrder = new com.shrct.businessprocess.purchase.impl.RCPurchaseOrderImpl(
    	purchaseRequest
    );
    objectFactory.create(rCPurchaseOrder);
    return rCPurchaseOrder;			
    
  }
  
  public static RCPurchaseOrder createRCPurchaseOrderImpl(final RCProductPurchaseRequest purchaseRequest, final IObjectFactory objectFactory, final Procedure1<RCPurchaseOrder> init) {
    com.shrct.businessprocess.purchase.RCPurchaseOrder rCPurchaseOrder = new com.shrct.businessprocess.purchase.impl.RCPurchaseOrderImpl(
    	purchaseRequest
    );
    objectFactory.create(rCPurchaseOrder);
    init.apply(rCPurchaseOrder);
    
    return rCPurchaseOrder;			
    
  }
  
  public RCPurchaseOrderProxy toProxy() {
    com.shrct.businessprocess.purchase.proxy.RCPurchaseOrderProxy proxy = new com.shrct.businessprocess.purchase.proxy.RCPurchaseOrderProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    proxy.setStatus(this.getStatus().name());
    return proxy;
    
  }
}
